package IOLearn;



import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author Lenl
 * @version v1.0
 * @create 2021-06-06 0:17
 * @description 使用流实现图片的加密
 *
 */
public class PicEncrypt {
    @Test
    public void Pictest() throws IOException {
        String picsrc="D:\\Java Learn\\IOLearnTestFile\\king exit\\提亚拉.jpg";
        String dest="D:\\Java Learn\\IOLearnTestFile\\picturetest\\加密.jpg";
        FileInputStream fi=new FileInputStream(picsrc);
        FileOutputStream fo=new FileOutputStream(dest);

        byte[] buffer=new byte[20];
        int len;
        while ((len=fi.read(buffer))!=-1){
            //字节数组进行修改,增强循环无法修改本体
            for(int i=0;i<len;i++){
                buffer[i]=(byte)(buffer[i]^5);
            }
            fo.write(buffer,0,len);
        }
        fo.close();
        fi.close();
    }

    //图片的解密
    @Test
    public void test2() throws IOException {
        String picsrc="D:\\Java Learn\\IOLearnTestFile\\picturetest\\加密.jpg";
        String dest="D:\\Java Learn\\IOLearnTestFile\\picturetest\\解密-提亚拉.jpg";
        FileInputStream fi=new FileInputStream(picsrc);
        FileOutputStream fo=new FileOutputStream(dest);

        byte[] buffer=new byte[20];
        int len;
        while ((len=fi.read(buffer))!=-1){
            //字节数组进行修改,增强循环无法修改本体
            for(int i=0;i<len;i++){
                buffer[i]=(byte)(buffer[i]^5);
            }
            fo.write(buffer,0,len);
        }
        fo.close();
        fi.close();
    }
}
